package com.tang.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.tang.entity.Student;
import com.tang.mapper.StudentMapper;
import com.tang.service.StudentService;
import com.tang.utils.MyBatisUtils;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @Author TangBowen
 * @create 2021/6/7 14:44
 */
public class StudentServiceImpl implements StudentService {

    @Override
    public Page selectByPage(Integer currentPage, Integer pageSize) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
        Page page = PageHelper.startPage(currentPage,pageSize);
        mapper.selectAll();
        if(sqlSession != null) {
            sqlSession.close();
        }
        return page;
    }

    /**
     * 添加学生逻辑
     * @param stu
     */
    @Override
    public void addStu(Student stu) {
        SqlSession sqlSession = MyBatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);
            mapper.addStu(stu);
            if(sqlSession != null) {
                sqlSession.close();
            }
    }

    @Override
    public void updateStu(Student stu) {
        SqlSession session = MyBatisUtils.getSqlSession();
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        studentMapper.updateStu(stu);
        if(session != null) {
            session.close();
        }
    }

    @Override
    public void deleteStu(String number) {
        SqlSession session = MyBatisUtils.getSqlSession();
        StudentMapper studentMapper = session.getMapper(StudentMapper.class);
        studentMapper.deleteStu(number);
        if(session != null) {
            session.close();
        }
    }

}
